package com.hailiao.beidou.message.data.db.entity

import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import androidx.room.Relation
import com.d10ng.common.coordinate.toLatitudeString
import com.d10ng.common.coordinate.toLongitudeString
import com.hailiao.beidou.message.data.db.constant.SendStatus

/**
 * 消息详情
 *
 * @Author: D10NG
 * @Time: 2021/12/29 16:38
 */
@Entity(
    tableName = "message_table",
    indices = [Index(value = ["number", "isSendOut", "status", "isLocation"])]
)
data class MessageEntity(

    @PrimaryKey(autoGenerate = true)
    var id: Long = 0,

    var number: String = "",
    // 创建时间
    var createTime: Long = 0,
    // 发送时间，与创建时间做区分，是为了再重发消息时能记录新的发送时间，而不改变原有的创建时间
    var sendTime: Long = 0,
    // 是否为本机发送出去的消息
    var isSendOut: Boolean = true,
    // 消息状态
    var status: Int = SendStatus.SUCCESS.value,
    // 发送内容
    var content: String = "",
    // 是否为定位消息
    val isLocation: Boolean = false,
    // 定位信息
    @Embedded(prefix = "loc_")
    var location: Location? = null
) {

    data class Location(
        var latitude: Double = 0.0,
        var longitude: Double = 0.0,
        var altitude: Double = 0.0,
    ) {
        fun toShow(): String {
            val pattern = "Fd°m′S.ss″"
            return "${latitude.toLatitudeString(pattern)},${longitude.toLongitudeString(pattern)}"
        }
    }
}

/**
 * 模糊搜索时匹配的消息
 * @property number String 联系人号码
 * @property name String 联系人名称
 * @property count Int 匹配的消息数
 * @constructor
 */
data class MatchedMessageCount(
    val number: String,
    val name: String,
    val count: Int
)

/**
 * 对话框信息 + 联系人信息
 *
 * @Author: D10NG
 * @Time: 2021/12/29 16:36
 */
data class MessageWithContactEntity(
    @Embedded
    val message: MessageEntity,

    @Relation(
        parentColumn = "number",
        entityColumn = "number"
    )
    val contact: ContactEntity?
)
